/**
 * Monitor special variables
 */
import {createProxy} from '../utils/util'
import Store from '../store/index'
import method from '../global/method'
import { toJson } from '../global/api'
import $ from 'jquery'

let undoTimer,redoTimer
function undoAccessible(len) {
    clearTimeout(undoTimer)
    undoTimer = setTimeout(() => {
        $('#luckysheet-icon-undo')[len ? 'removeClass' : 'addClass']('disabled')
    }, 10)
}
function redoAccessible(len) {
    clearTimeout(redoTimer)
    redoTimer = setTimeout(() => {
        $('#luckysheet-icon-redo')[len ? 'removeClass' : 'addClass']('disabled')
    }, 10)
}

const initListener = function(){
    // createProxy(Store,['jfredo']);
    createProxy(Store, 'jfredo',(target, property, val)=>{
        if (property !== 'length') {
            //  钩子函数
            method.createHookFunction('updated',val)
        }
        undoAccessible(Store.jfredo.length)
    } )
    createProxy(Store, 'jfundo',()=>{
        redoAccessible(Store.jfundo.length)
    } )



    createProxy(Store, 'asyncLoad', (target, property, val)=>{
        if(property === 'length' && val === 0){
            method.createHookFunction('workbookCreateAfter', toJson())
        }
    })
}

export {
    initListener
}
